/**   
 * @Title: ParamDataDaoImpl.java 
 * CopyRight(c)2010-DMX 
 */
package com.dmx.fw.dao.hibernateimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.dmx.fw.bean.mapping.pojo.ParameterData;
import com.dmx.fw.dao.hibernateimpl.JdbcDaoImpl;
import com.dmx.fw.dao.interfaces.ParamDataDao;

/**
 * 
 * @Description: ParamData Dao Implement
 * @author yuzhixin 
 * @date 2010-3-28 
 * modified By:
 * modified Date:
 * Why & What is modified:
 * @version V1.0
 *
 */
public class ParamDataDaoImpl extends JdbcDaoImpl implements ParamDataDao{
	private final static Logger log = Logger.getLogger(ParamDataDaoImpl.class);
	
	/**
	 * <p>Description: 创建ParameterData</p> 
	 * @param paramData
	 * @return 
	 * @see @see com.dmx.fw.dao.interfaces.ParamDataDao#create(com.dmx.fw.bean.mapping.pojo.ParameterData)
	 */
	public Long create(ParameterData paramData ){
		return (Long)this.getHibernateTemplate().save(paramData);
	}
	/**
	 * 
	 * <p>Description: 根据ID,删除ParameterData</p> 
	 * @param paramDataId 
	 * @see @see com.dmx.fw.dao.interfaces.ParamDataDao#delete(java.lang.String)
	 */
	public void delete(String paramDataId){
		this.removeObject(ParameterData.class, Long.valueOf(paramDataId));
	}
	
	/**
	 * 
	 * <p>Description: 更新ParameterData</p> 
	 * @param paramData 
	 * @see @see com.dmx.fw.dao.interfaces.ParamDataDao#update(com.dmx.fw.bean.mapping.pojo.ParameterData)
	 */
	public void update(ParameterData paramData){
		this.getHibernateTemplate().update(paramData);
	}

	/**
	 * 
	 * <p>Description: 根据ID加载ParameterData</p> 
	 * @param paramDataId
	 * @return 
	 * @see @see com.dmx.fw.dao.interfaces.ParamDataDao#getParamData(java.lang.String)
	 */
	public ParameterData getParamData(String paramDataId){
		return (ParameterData)this.getObject(ParameterData.class, Long.valueOf(paramDataId));
	}
	
	/**
	 * 
	 * <p>Description: 判断某一typeCode下name是否唯一</p> 
	 * @param typeCode
	 * @param name
	 * @param parmDataId
	 * @return 
	 * @see @see com.dmx.fw.dao.interfaces.ParamDataDao#nameUniqueCheck(java.lang.String, java.lang.String, long)
	 */
	public boolean nameUniqueCheck(final String typeCode,final String name,final long parmDataId){		
		Boolean bResult =  (Boolean) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
        		Connection conn = null;
        		int iRes = 0;
        		ResultSet rs = null;
        		PreparedStatement pst = null;
        		String sql = "select count(*) from SYSTEM_PARM_DATA where TYPE_CODE=? and ID<>? and NAME=?";
        		try {
        			conn = session.connection();
        			pst = conn.prepareStatement(sql,
        					ResultSet.TYPE_SCROLL_INSENSITIVE,
        					ResultSet.CONCUR_READ_ONLY);
        			pst.setString(1, typeCode);
        			pst.setLong(2, parmDataId);
        			pst.setString(3, name);
        			rs = pst.executeQuery();
        			if (rs.next())
        				iRes = rs.getInt(1);
        		} catch (SQLException e) {
        			log.error(e);
        		} finally {
        			closeResultSet(rs);
        			closeStatement(pst);
        		}
        		if (iRes>0){
        			return false;
        		}
        		return true;
            }
        });
		
		return bResult.booleanValue();		
	}
	
	/**
	 * 
	 * <p>Description: 判断某一typeCode下code,name是否唯一</p> 
	 * @param typeCode
	 * @param code
	 * @param name
	 * @return 
	 * @see @see com.dmx.fw.dao.interfaces.ParamDataDao#uniqueCheck(java.lang.String, java.lang.String, java.lang.String)
	 */
	public boolean uniqueCheck(final String typeCode,final String code,final String name){
		Boolean bResult =  (Boolean) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
        		Connection conn = null;
        		int iRes = 0;
        		ResultSet rs = null;
        		PreparedStatement pst = null;
        		String sql = "select count(*) from SYSTEM_PARM_DATA where TYPE_CODE=? and CODE=? and NAME=?";
        		try {
        			conn = session.connection();
        			pst = conn.prepareStatement(sql,
        					ResultSet.TYPE_SCROLL_INSENSITIVE,
        					ResultSet.CONCUR_READ_ONLY);
        			pst.setString(1, typeCode);
        			pst.setString(2, code);
        			pst.setString(3, name);
        			rs = pst.executeQuery();
        			if (rs.next())
        				iRes = rs.getInt(1);
        		} catch (SQLException e) {
        			log.error(e);
        		} finally {
        			closeResultSet(rs);
        			closeStatement(pst);
        		}
        		if (iRes>0){
        			return false;
        		}
        		return true;
            }
        });
		
		return bResult.booleanValue();		
	}
}
